<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>地理位置距离(geo_distance)查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl-geo-distance-query.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-geo-distance-query.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-geo-distance-query.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl-geo-distance-query.html" rel="nofollow" target="_blank">../en/query-dsl-geo-distance-query.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="query-dsl.html">查询领域特定语言(Query DSL)</a></span>
»
<span class="breadcrumb-link"><a href="geo-queries.html">地理位置(geo)查询</a></span>
»
<span class="breadcrumb-node">地理位置距离(geo_distance)查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="query-dsl-geo-bounding-box-query.html">« 地理位置边界框(geo_bounding_box)查询</a>
</span>
<span class="next">
<a href="query-dsl-geo-polygon-query.html">地理位置多边形(geo-polygon)查询 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="query-dsl-geo-distance-query"></a>地理位置距离(geo_distance)查询
</h2>
</div></div></div>

<p>过滤仅包含与一个地理位置坐标在指定距离内匹配的文档。假设有以下映射和索引文档：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /my_locations
{
    "mappings": {
        "properties": {
            "pin": {
                "properties": {
                    "location": {
                        "type": "geo_point"
                    }
                }
            }
        }
    }
}

PUT /my_locations/_doc/1
{
    "pin" : {
        "location" : {
            "lat" : 40.12,
            "lon" : -71.34
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/180.console"></div>
<p>然后，可以使用<code class="literal">geo_distance</code>过滤器执行下面这个简单的查询：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/181.console"></div>
<h4>
<a id="_accepted_formats"></a>接受的格式
</h4>
<p>与<code class="literal">geo_point</code>类型可以接受地理坐标点的不同表示方式非常相似，过滤器也可以接受它：</p>
<h5>
<a id="_lat_lon_as_properties_2"></a>作为 lat lon 属性
</h5>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : {
                        "lat" : 40,
                        "lon" : -70
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/182.console"></div>
<h5>
<a id="_lat_lon_as_array_2"></a>作为 lat lon 数组
</h5>
<p>格式为<code class="literal">[lon, lat]</code>，注意，此处 lon/lat 的顺序是为了符合<a href="http://geojson.org/" class="ulink" target="_top">GeoJSON</a>。</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : [-70, 40]
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/183.console"></div>
<h5>
<a id="_lat_lon_as_string_2"></a>作为 lat lon 字符串
</h5>
<p>格式为<code class="literal">lat,lon</code>。</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : "40,-70"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/184.console"></div>
<h5>
<a id="_geohash_2"></a>geohash
</h5>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /my_locations/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "12km",
                    "pin.location" : "drm3btev3e86"
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/185.console"></div>
<h4>
<a id="_options"></a>选项
</h4>
<p>以下是过滤器上允许的选项：</p>
<div class="informaltable">
<table border="0" cellpadding="4px">
<colgroup>
<col>
<col>
</colgroup>
<tbody valign="top">
<tr>
<td valign="top">
<p>
<code class="literal">distance</code>
</p>
</td>
<td valign="top">
<p>
以指定位置为圆心的圆的半径。

落入这个圆内的点被认为是匹配的。

可以用各种单位指定<code class="literal">distance</code>。参考<a class="xref" href="common-options.html#distance-units" title="Distance Units">距离单位</a>。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">distance_type</code>
</p>
</td>
<td valign="top">
<p>
如何计算距离。可以是<code class="literal">arc</code>(弧形。默认值)，也可以是<code class="literal">plane</code>(平面。计算更快，但在长距离和接近极点时不准确)。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">_name</code>
</p>
</td>
<td valign="top">
<p>
可选，用来标识查询名称的字段。
</p>
</td>
</tr>
<tr>
<td valign="top">
<p>
<code class="literal">validation_method</code>
</p>
</td>
<td valign="top">
<p>
设置为<code class="literal">IGNORE_MALFORMED</code>以接受无效的纬度或经度值的 geo point，设置为<code class="literal">COERCE</code>以尝试推断正确的纬度或经度。(默认为 <code class="literal">STRICT</code>)。
</p>
</td>
</tr>
</tbody>
</table>
</div>
<h4>
<a id="_geo_point_type_2"></a>geo_point 类型
</h4>
<p>过滤器<span class="strong strong"><strong>要求</strong></span>在相关字段中设置<code class="literal">geo_point</code>类型。</p>
<h4>
<a id="_multi_location_per_document_2"></a>一个文档包含多个位置
</h4>
<p>
<code class="literal">geo_distance</code>过滤器可以处理每个文档的多个位置/坐标点。

一旦单个位置/坐标点与过滤器匹配，该文档就将包含在过滤器中。
</p>
<h4>
<a id="_ignore_unmapped_2"></a>忽略未映射的字段 (ignore unmapped)
</h4>

<p>
如果<code class="literal">ignore_unmapped</code>选项设置为<code class="literal">true</code>，将忽略未映射的字段，并且此查询不会匹配任何文档。

这在查询可能具有不同映射的多个索引时非常有用。

当设置为<code class="literal">false</code>(默认值)时，如果字段未映射，查询将抛出异常。
</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="query-dsl-geo-bounding-box-query.html">« 地理位置边界框(geo_bounding_box)查询</a>
</span>
<span class="next">
<a href="query-dsl-geo-polygon-query.html">地理位置多边形(geo-polygon)查询 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>